/**
 * 
 */
package edu.scu.lijobs.android;

import java.io.IOException;

import com.google.android.gcm.GCMRegistrar;

import android.app.AlertDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;


import edu.scu.lijobs.android.R.layout;

/**
 * Representing TabNotificationSetting class
 * @author dave
 *
 */
public class TabNotificationSettingsFragment extends Fragment {
	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
	 */
	
	private LinearLayout linearLayout;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (container == null) {
          
            return null;
        }
		
		
		
		
		
		UiHelper.notificationSettingsFragment = this;
		this.linearLayout = (LinearLayout)inflater.inflate(layout.tab_notification_settings_layout, container, false);
		Button saveSettings = (Button)linearLayout.findViewById(R.id.saveSettingsButton);
//		Button cancel = (Button) linearLayout.findViewById(R.id.cancelButton);
//		
		saveSettings.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				
				
				saveSettings();
				
				
			}
		});
		
		
		EditText jobTitileTextBox = (EditText)linearLayout.findViewById(R.id.jobTitleTextBox);
		jobTitileTextBox.setText("Software Engineer");
		EditText companiesTextBox = (EditText)linearLayout.findViewById(R.id.companiesTextBox);
		companiesTextBox.setText("LinkedIn");
		
		EditText zipcodeTextBox =(EditText)linearLayout.findViewById(R.id.zipcodeTextBox);
		zipcodeTextBox.setText("94560");
		return linearLayout;
	}
	
	private void saveSettings() {
		EditText jobTitileTextBox = (EditText)linearLayout.findViewById(R.id.jobTitleTextBox);
		EditText companiesTextBox = (EditText)linearLayout.findViewById(R.id.companiesTextBox);
		EditText zipcodeTextBox =(EditText)linearLayout.findViewById(R.id.zipcodeTextBox);
		
		if(jobTitileTextBox.getText().toString().length() == 0) {
			 new AlertDialog.Builder(getActivity()).setTitle("Error").setMessage("Job Title is required").setNeutralButton("Close", null).show();
			 return;
		} 
		
		if(zipcodeTextBox.getText().toString().length() == 0) {
			 new AlertDialog.Builder(getActivity()).setTitle("Error").setMessage("Zipcode is required").setNeutralButton("Close", null).show(); 
			 return;
		
		}
		
		
		
		AsyncTask<Void, Void, Void> saveSettingTask = new AsyncTask<Void, Void, Void>() {

             @Override
             protected Void doInBackground(Void... params) {
            	 EditText jobTitileTextBox = (EditText)linearLayout.findViewById(R.id.jobTitleTextBox);
         		 EditText companiesTextBox = (EditText)linearLayout.findViewById(R.id.companiesTextBox);
         		 EditText zipcodeTextBox =(EditText)linearLayout.findViewById(R.id.zipcodeTextBox);
         		
         		String jobTitle = jobTitileTextBox.getText().toString();
         		String companies = companiesTextBox.getText().toString();
         		String zipcode = zipcodeTextBox.getText().toString();
         		try {
					ServerUtilities.saveSettings(jobTitle, companies, zipcode);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
         		return null;
             }

             @Override
             protected void onPostExecute(Void result) {
            	 new AlertDialog.Builder(getActivity()).setTitle("Save Settings").setMessage("Save Settings completed").setNeutralButton("Close", null).show();  
             }

         };
        saveSettingTask.execute(null,null,null);
		
		
		
	}
}
